package com.weipu.common.facade.model;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.weipu.common.constants.ServerConstant;

/**
 * @author Tony 描述：用于接收结算金额接口返回数据的JavaBean
 */
public class PreviewOrderModel {
	// 总优惠额度
	private double allDiscountTotal;
	// 应付额度
	private double grandTotal;
	// 货币代码
	private String currencyCode;
	// 货币单位
	private String currencyCodeStr;
	// 订单总额
	private double totalAmount;
	// 产品总额
	private double goodsTotal;
	// 运费
	private double shippingFee;
	// 运输方式取值为：0、1、2
	private int shippingMethod;// 取值为：0、1、2
	private String shippingMethodStr;// 取值为SuperSaver、Standard、Expedited
	// 订单产品列表
	private ArrayList<OrderProductModel> Lines;

	// 已经使用的Coupon列表、
	private ArrayList<CouponModel> appliedCoupons;

	// 订单可用的手动Coupon列表
	private ArrayList<CouponModel> usableCoupons;

	// 赠送Sku列表
	private ArrayList<OrderProductModel> allGiftSkus;

	private CouponError couponError;
	
	private float ExpectReturnSCAmount;

	public ArrayList<OrderProductModel> getAllGiftSkus() {
		return allGiftSkus;
	}

	// 判断本地购物车上传到服务器时,是否存在互斥商品
	private boolean IsMerged;

	public boolean isMerged() {
		return IsMerged;
	}

	public void setMerged(boolean merged) {
		IsMerged = merged;
	}

	public void setAllGiftSkus(ArrayList<OrderProductModel> allGiftSkus) {
		this.allGiftSkus = allGiftSkus;
	}

	public double getAllDiscountTotal() {
		return allDiscountTotal;
	}

	public void setAllDiscountTotal(double allDiscountTotal) {
		this.allDiscountTotal = allDiscountTotal;
	}

	public String getCurrencyCode() {
		return currencyCode;
	}

	public void setCurrencyCode(String currencyCode) {
		this.currencyCode = currencyCode;
	}

	public String getCurrencyCodeStr() {
		return currencyCodeStr;
	}

	public void setCurrencyCodeStr(String currencyCodeStr) {
		this.currencyCodeStr = currencyCodeStr;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public double getGoodsTotal() {
		return goodsTotal;
	}

	public void setGoodsTotal(double goodsTotal) {
		this.goodsTotal = goodsTotal;
	}

	public double getShippingFee() {
		return shippingFee;
	}

	public void setShippingFee(double shippingFee) {
		this.shippingFee = shippingFee;
	}

	public int getShippingMethod() {
		return shippingMethod;
	}

	public void setShippingMethod(int shippingMethod) {
		this.shippingMethod = shippingMethod;
	}

	public String getShippingMethodStr() {
		return shippingMethodStr;
	}

	public void setShippingMethodStr(String shippingMethodStr) {
		this.shippingMethodStr = shippingMethodStr;
	}

	public CouponError getCouponError() {
		return couponError;
	}

	public void setCouponError(CouponError couponError) {
		this.couponError = couponError;
	}

	public float getExpectReturnSCAmount() {
        return ExpectReturnSCAmount;
    }

    public void setExpectReturnSCAmount(float expectReturnSCAmount) {
        ExpectReturnSCAmount = expectReturnSCAmount;
    }

    public ArrayList<OrderProductModel> getLines() {
		return Lines;
	}

	public void setLines(ArrayList<OrderProductModel> lines) {
		Lines = lines;
	}

	public ArrayList<CouponModel> getAppliedCoupons() {
		return appliedCoupons;
	}

	public void setAppliedCoupons(ArrayList<CouponModel> appliedCoupons) {
		this.appliedCoupons = appliedCoupons;
	}

	public ArrayList<CouponModel> getUsableCoupons() {
		return usableCoupons;
	}

	public void setUsableCoupons(ArrayList<CouponModel> usableCoupons) {
		this.usableCoupons = usableCoupons;
	}

	public double getOrderDiscount() {
		return allDiscountTotal;
	}

	public void setOrderDiscount(double allDiscountTotal) {
		this.allDiscountTotal = allDiscountTotal;
	}

	public double getGrandTotal() {
		return grandTotal;
	}

	public void setGrandTotal(double grandTotal) {
		this.grandTotal = grandTotal;
	}

	/**
	 * 将json对象转化为javabean的方法
	 * 
	 * @return javabean
	 */
	public static PreviewOrderModel json2PreviewOrderModel(JSONObject data) {
		PreviewOrderModel previewOrderModel = null;
		if (data != null) {
			previewOrderModel = new PreviewOrderModel();

			previewOrderModel
					.setAllDiscountTotal(data
							.optDouble(ServerConstant.PreviewOrder.PreviewOrder_AllDiscountTotal));
			JSONArray appliedCouponsArray = data
					.optJSONArray(ServerConstant.PreviewOrder.PreviewOrder_AppliedCoupons);
			if (appliedCouponsArray != null && appliedCouponsArray.length() > 0) {
				ArrayList<CouponModel> list = new ArrayList<CouponModel>();
				for (int i = 0; i < appliedCouponsArray.length(); i++) {
					JSONObject object = (JSONObject) appliedCouponsArray.opt(i);
					list.add(CouponModel.jsonObject2Coupon(object));
				}
				previewOrderModel.setAppliedCoupons(list);
			}
			JSONObject couponErrorJson = data
					.optJSONObject(ServerConstant.PreviewOrder.PREVIEWORDER_COUPON_ERROR);
			if (null != couponErrorJson) {
				CouponError errorCoupon = new CouponError();
				if (couponErrorJson
						.optString(ServerConstant.PreviewOrder.PREVIEWORDER_COUPON_ERROR_CODE) != JSONObject.NULL
						&& !couponErrorJson
								.isNull(ServerConstant.PreviewOrder.PREVIEWORDER_COUPON_ERROR_CODE)) {
					errorCoupon
							.setErrorCode(couponErrorJson
									.optString(ServerConstant.PreviewOrder.PREVIEWORDER_COUPON_ERROR_CODE));
				}
				if (couponErrorJson
						.optString(ServerConstant.PreviewOrder.PREVIEWORDER_COUPON_ERROR_SHORT_MESSAGE) != JSONObject.NULL
						&& !couponErrorJson
								.isNull(ServerConstant.PreviewOrder.PREVIEWORDER_COUPON_ERROR_SHORT_MESSAGE)) {
					errorCoupon
							.setShortMessgae(couponErrorJson
									.optString(ServerConstant.PreviewOrder.PREVIEWORDER_COUPON_ERROR_SHORT_MESSAGE));
				}
				if (couponErrorJson
						.optString(ServerConstant.PreviewOrder.PREVIEWORDER_COUPON_ERROR_LONG_MESSAGE) != JSONObject.NULL
						&& !couponErrorJson
								.isNull(ServerConstant.PreviewOrder.PREVIEWORDER_COUPON_ERROR_LONG_MESSAGE)) {
					errorCoupon
							.setLongMessage(couponErrorJson
									.optString(ServerConstant.PreviewOrder.PREVIEWORDER_COUPON_ERROR_LONG_MESSAGE));
				}
				previewOrderModel.setCouponError(errorCoupon);
			}
			
			previewOrderModel.setExpectReturnSCAmount((float) data.optDouble("ExpectReturnSCAmount"));

			previewOrderModel
					.setCurrencyCode(data
							.optString(ServerConstant.PreviewOrder.PreviewOrder_CurrencyCode));
			previewOrderModel
					.setCurrencyCodeStr(data
							.optString(ServerConstant.PreviewOrder.PreviewOrder_CurrencyCodeStr));
			previewOrderModel
					.setGrandTotal(data
							.optDouble(ServerConstant.PreviewOrder.PreviewOrder_GrandTotal));
			JSONArray LinesArray = data
					.optJSONArray(ServerConstant.PreviewOrder.PreviewOrder_Lines);
			if (LinesArray != null && LinesArray.length() > 0) {
				ArrayList<OrderProductModel> list = new ArrayList<OrderProductModel>();
				for (int i = 0; i < LinesArray.length(); i++) {
					JSONObject object = (JSONObject) LinesArray.opt(i);
					list.add(new OrderProductModel()
							.json2OrderProductmModel(object));
				}
				previewOrderModel.setLines(list);
			}
			previewOrderModel
					.setOrderDiscount(data
							.optDouble(ServerConstant.PreviewOrder.PreviewOrder_AllDiscountTotal));
			previewOrderModel
					.setShippingFee(data
							.optDouble(ServerConstant.PreviewOrder.PreviewOrder_ShippingFee));
			previewOrderModel
					.setShippingMethod(data
							.optInt(ServerConstant.PreviewOrder.PreviewOrder_ShippingMethod));
			previewOrderModel
					.setShippingMethodStr(data
							.optString(ServerConstant.PreviewOrder.PreviewOrder_ShippingMethodStr));
			previewOrderModel
					.setTotalAmount(data
							.optDouble(ServerConstant.PreviewOrder.PreviewOrder_TotalAmount));
			previewOrderModel
					.setGoodsTotal(data
							.optDouble(ServerConstant.PreviewOrder.PREVIEWORDER_GOODS_TOTAL));
			JSONArray usableCouponsArray = data
					.optJSONArray(ServerConstant.PreviewOrder.PreviewOrder_UsableCoupons);
			if (usableCouponsArray != null && usableCouponsArray.length() > 0) {
				ArrayList<CouponModel> list = new ArrayList<CouponModel>();
				for (int i = 0; i < usableCouponsArray.length(); i++) {
					JSONObject object = (JSONObject) usableCouponsArray.opt(i);
					list.add(CouponModel.jsonObject2Coupon(object));
				}
				previewOrderModel.setUsableCoupons(list);
			}

			JSONArray array = data
					.optJSONArray(ServerConstant.PreviewOrder.PreviewOrder_AllGiftSkus);
			if (array != null && array.length() > 0) {
				ArrayList<OrderProductModel> list = new ArrayList<OrderProductModel>();
				for (int i = 0; i < array.length(); i++) {
					OrderProductModel model = new OrderProductModel();
					JSONObject object = (JSONObject) array.opt(i);
					model.setSku(object.optInt("Sku"));
					model.setQuantity(object.optInt("Qty"));
					model.setPrice((float)object.optDouble("Price"));
					model.setWebUrl(object.optString("WebUrl"));
					model.setShortHeadLine1(object.optString("ShortHeadLine"));
					model.setBigPictureURL(object.optJSONArray("BigPictureURL").optString(0));
					model.setMediumPictureURL(object.optJSONArray("MediumPictureURL").optString(0));
					model.setSmallPictureURL(object.optJSONArray("SmallPictureURL").optString(0));
					model.setGift(true);
					list.add(model);
				}
				previewOrderModel.setAllGiftSkus(list);
			}
		}
		return previewOrderModel;
	}
}
